<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.dao.UserDao"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.dao.DepartmentDao"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.util.Constants"%>

<%
{
User theUser = (User) session.getAttribute(Constants.SESSION_USER);
DepartmentDao theDepartDao = DaoCreator.createDepartmentDao();
List<User> userList = theDepartDao.getUserIDsByDepartment2IDandRoleType(theUser.getDepartment2ID(), Constants.ROLE_TYPE_KEYUAN);
%>
<%
if (userList.size() > 0) {
%>
<script type="text/javascript">
var userIDList = [];
var count = 0;
</script>

<div style="overflow: auto; height: 400px">
<table width="700" border="1" cellpadding="0"
		cellspacing="0">
	<%
	for (int i = 0; i < 100; i++) {
		boolean isBreak = false;
	%>
	<tr>
		<%
		for (int j = 0; j < 8; j++) {
			int index = i * 8 + j;
			if (index >= userList.size()) {
				isBreak = true;
				break;
			}
			User user = userList.get(index);
		%>
		<td align="center" id="userTD<%=user.getId()%>">
			<input id="manageGroupUserCheck<%=user.getId()%>" name="manageGroupUserCheck" type="checkbox" value="<%=user.getId()%>" />
			<script type="text/javascript">
			userIDList[count++] = <%=user.getId()%>
			</script>
			<span><%=user.getName()%></span>
		</td>
		<%
		}
		%>
	</tr>
	<%
		if (isBreak) {
			break;
		}
	}
	%>
</table>
</div>
<input type="button" style="margin-left: 40px" value="确 定" onclick="addManageGroupUser(1)" />
<input type="button" style="margin-left: 20px" value="全 选" onclick="checkAll(true)" />
<input type="button" style="margin-left: 20px" value="全不选" onclick="checkAll(false)" />
<%
}
}
%>

<script type="text/javascript">
function addManageGroupUser(manageGroupID) {
	openWaitingDialog('waitingDialog');
	
	var userIDs = '';
	for (var i=0; i != userIDList.length; i++) {
		var userID = userIDList[i];
		var element = document.getElementById('manageGroupUserCheck'+userID);
		if (element != null && element.checked) {
			if (userIDs == '') {
				userIDs = element.value;
			} else {
				userIDs = userIDs + ',' + element.value;
			}
		}
	}
	
	$.dialog.get('manageGroupUserListDialog').close();
	
	$.ajax({
		type:'post',
		url:'choose_manage_group_user_process.jsp',
		data:{
			id: userIDs,
			manageGroupID: manageGroupID
		},
		success:function(data,state,xmlhttp){
			var obj = null;
			try {
				obj = eval('(' + data + ')');
			} catch (e) {
			}
			if (typeof(obj.isSuccess) != 'undefined') {
				closeWaitingDialog('waitingDialog');
				if (obj.isSuccess == 'true') {
					loadPageSection('<%=request.getContextPath()%>/choose_manage_group_user.jsp', 'chooseManageGroupUserDiv');
				} else {
					$.dialog.alert(obj.error);
				}
			}
		}
	});
}

function checkAll(isChecked) {
	for (var i=0; i != userIDList.length; i++) {
		var userID = userIDList[i];
		var element = document.getElementById('manageGroupUserCheck'+userID);
		if (element != null) {
			element.checked = isChecked;
		}
	}
}
</script>
